package com.group11.job.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.group11.common.pojo.Enterprise;
import com.group11.common.pojo.ResponseResult;
import com.group11.common.pojo.User;
import com.group11.common.vo.EnterpriseVo;
import com.group11.job.domain.Job;
import com.group11.job.service.JobService;
import com.group11.job.service.UserService;
import com.group11.job.vo.JobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class JobController {

    @Autowired
    private JobService jobService;

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public ResponseResult<Void> addJob(@RequestHeader int id, @RequestBody Job job) {
        ResponseResult<User> responseResult = userService.getUserById(id);
        if ("1".equals(responseResult.getData().getType())) {
            return ResponseResult.FAILED;
        }

        ResponseResult<EnterpriseVo> enterpriseById = userService.getEnterpriseByUserId(id);
        if (enterpriseById.getCode() != 200) {
            return ResponseResult.FAILED;
        }
        job.setEntId(enterpriseById.getData().getId());
        job.setFavorNum(0);
        job.setViewNum(0);
        job.setDeliveNum(0);
        job.setPubTime(LocalDateTime.now().toString());
        job.setRefreshTime(LocalDateTime.now().toString());
        job.setStatus("0");
        job.setIsDeleted("0");
        jobService.save(job);
        return ResponseResult.SUCCESS;
    }


    @PostMapping("list")
    public ResponseResult<List<JobVo>> getJobList() {
        return new ResponseResult<>(jobService.getJobs());
    }

    @PostMapping("delete")
    public ResponseResult<Void> deleteJob(@RequestParam int id) {
        UpdateWrapper<Job> jobUpdateWrapper = new UpdateWrapper<>();
        jobUpdateWrapper.eq("id", id).set("is_deleted", "1");
        boolean update = jobService.update(jobUpdateWrapper);
        if (update) {
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.FAILED;
    }

    @PostMapping("/{id}")
    public ResponseResult<?> getJobById(@PathVariable int id) {
        JobVo job = jobService.getJob(id);
        if (job == null) {
            return ResponseResult.FAILED;
        }
        return new ResponseResult<>(job);
    }

    @PostMapping("refresh")
    public ResponseResult<Void> refreshJob(@RequestParam int id) {
        UpdateWrapper<Job> jobUpdateWrapper = new UpdateWrapper<>();
        jobUpdateWrapper.eq("id", id).set("refresh_time", LocalDateTime.now().toString());
        boolean update = jobService.update(jobUpdateWrapper);
        if (update) {
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.FAILED;
    }

    @PostMapping("favor")
    public ResponseResult<Void> favorJob(@RequestHeader("id") int userid, @RequestParam int id) {
        if (jobService.favorJob(userid, id)) {
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.FAILED;
    }

    @GetMapping("getByEntId")
    public ResponseResult<List<Job>> getByEntId(@RequestParam int id) {
        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("ent_id", id).eq("is_deleted", "0");
        return new ResponseResult<>(jobService.list(jobQueryWrapper));
    }

}
